home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 15 / CU Amiga Magazine's Super CD-ROM 15 (1997)(EMAP Images)(GB)[!][issue 1997-10].iso / CUCD / Graphics / Ghostscript / source / gdevpng.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-07  |  8.0 KB  |  266 lines

  1. /* Copyright (C) 1995, 1996, 1997 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gdevpng.c */
  20. /* PNG (Portable Network Graphics) Format.  Pronounced "ping". */
  21. /* lpd 1997-5-7: added PNG_LIBPNG_VER conditional for operand types of
  22.  * dummy png_push_fill_buffer. */
  23. /* lpd 1997-4-13: Added PNG_NO_STDIO to remove library access to stderr. */
  24. /* lpd 1997-3-14: Added resolution (pHYs) to output. */
  25. /* lpd 1996-6-24: Added #ifdef for compatibility with old libpng versions. */
  26. /* lpd 1996-6-11: Edited to remove unnecessary color mapping code. */
  27. /* lpd (L. Peter Deutsch) 1996-4-7: Modified for libpng 0.88. */
  28. /* Original version by Russell Lang 1995-07-04 */
  29.  
  30. #include "gdevprn.h"
  31. #include "gdevpccm.h"
  32. #include "gscdefs.h"
  33.  
  34. #define PNG_INTERNAL
  35. #define PNG_NO_STDIO
  36. #include "png.h"
  37.  
  38. /* ------ The device descriptors ------ */
  39.  
  40. /*
  41.  * Default X and Y resolution.
  42.  */
  43. #define X_DPI 72
  44. #define Y_DPI 72
  45.  
  46. private dev_proc_print_page(png_print_page);
  47.  
  48. /* Monochrome. */
  49.  
  50. gx_device_printer far_data gs_pngmono_device =
  51.   prn_device(prn_std_procs, "pngmono",
  52.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  53.     X_DPI, Y_DPI,
  54.     0,0,0,0,            /* margins */
  55.     1, png_print_page);
  56.  
  57. /* 4-bit planar (EGA/VGA-style) color. */
  58.  
  59. private gx_device_procs png16_procs =
  60.   prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  61.     pc_4bit_map_rgb_color, pc_4bit_map_color_rgb);
  62. gx_device_printer far_data gs_png16_device =
  63.   prn_device(png16_procs, "png16",
  64.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  65.     X_DPI, Y_DPI,
  66.     0,0,0,0,            /* margins */
  67.     4, png_print_page);
  68.  
  69. /* 8-bit (SuperVGA-style) color. */
  70. /* (Uses a fixed palette of 3,3,2 bits.) */
  71.  
  72. private gx_device_procs png256_procs =
  73.   prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  74.     pc_8bit_map_rgb_color, pc_8bit_map_color_rgb);
  75. gx_device_printer far_data gs_png256_device =
  76.   prn_device(png256_procs, "png256",
  77.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  78.     X_DPI, Y_DPI,
  79.     0,0,0,0,            /* margins */
  80.     8, png_print_page);
  81.  
  82. /* 8-bit gray */
  83.  
  84. private gx_device_procs pnggray_procs =
  85.   prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  86.     gx_default_gray_map_rgb_color, gx_default_gray_map_color_rgb);
  87. gx_device_printer far_data gs_pnggray_device =
  88. { prn_device_body(gx_device_printer, pnggray_procs, "pnggray",
  89.            DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  90.            X_DPI, Y_DPI,
  91.            0,0,0,0,            /* margins */
  92.            1,8,255,0,256,0, png_print_page)
  93. };
  94.  
  95. /* 24-bit color. */
  96.  
  97. private gx_device_procs png16m_procs =
  98.   prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  99.     gx_default_rgb_map_rgb_color, gx_default_rgb_map_color_rgb);
  100. gx_device_printer far_data gs_png16m_device =
  101.   prn_device(png16m_procs, "png16m",
  102.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  103.     X_DPI, Y_DPI,
  104.     0,0,0,0,            /* margins */
  105.     24, png_print_page);
  106.  
  107.  
  108. /* ------ Private definitions ------ */
  109.  
  110. /* Write out a page in PNG format. */
  111. /* This routine is used for all formats. */
  112. private int
  113. png_print_page(gx_device_printer *pdev, FILE *file)
  114. {    int raster = gdev_prn_raster(pdev);
  115.     /* PNG structures */
  116.     byte *row = (byte *)gs_malloc(raster, 1, "png raster buffer");
  117.     png_struct *png_ptr =
  118.       gs_malloc(sizeof (png_struct), 1, "png structure");
  119.     png_info *info_ptr =
  120.       gs_malloc(sizeof (png_info), 1, "png info_ptr");
  121.     int height = pdev->height;
  122.     int depth = pdev->color_info.depth;
  123.     int y;
  124.     int code = 0;            /* return code */
  125.     const char *software_key = "Software";
  126.     char software_text[256];
  127.     png_text text_png;
  128.  
  129.     if ( row == 0 || png_ptr == 0 || info_ptr == 0 )
  130.       { code = gs_note_error(gs_error_VMerror);
  131.         goto done;
  132.       }
  133.  
  134.     /* set error handling */
  135.     if (setjmp(png_ptr->jmpbuf))
  136.     {
  137.         /* If we get here, we had a problem reading the file */
  138.         png_write_destroy(png_ptr);
  139.         code = gs_note_error(gs_error_VMerror);
  140.         goto done;
  141.     }
  142.  
  143.     /* initialize the structures */
  144.     png_info_init(info_ptr);
  145.     png_write_init(png_ptr);
  146.  
  147.     /* set up the output control */
  148.     png_init_io(png_ptr, file);
  149.  
  150.     /* set the file information here */
  151.     info_ptr->width = pdev->width;
  152.     info_ptr->height = pdev->height;
  153.     /* resolution is in pixels per meter vs. dpi */
  154.     info_ptr->x_pixels_per_unit =
  155.       (png_uint_32)(pdev->HWResolution[0] * (100.0 / 2.54));
  156.     info_ptr->y_pixels_per_unit =
  157.       (png_uint_32)(pdev->HWResolution[1] * (100.0 / 2.54));
  158.     info_ptr->phys_unit_type = PNG_RESOLUTION_METER;
  159.     info_ptr->valid |= PNG_INFO_pHYs;
  160.     switch(depth) {
  161.         case 24:
  162.             info_ptr->bit_depth = 8;
  163.         info_ptr->color_type = PNG_COLOR_TYPE_RGB;
  164.         break;
  165.         case 8:
  166.         info_ptr->bit_depth = 8;
  167.         if (gx_device_has_color(pdev))
  168.             info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
  169.         else
  170.             info_ptr->color_type = PNG_COLOR_TYPE_GRAY;
  171.         break;
  172.         case 4:
  173.         info_ptr->bit_depth = 4;
  174.         info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
  175.         break;
  176.         case 1:
  177.         info_ptr->bit_depth = 1;
  178.         info_ptr->color_type = PNG_COLOR_TYPE_GRAY;
  179.         /* invert monocrome pixels */
  180.            png_set_invert_mono(png_ptr);
  181.         break;
  182.     }
  183.  
  184.     /* set the palette if there is one */
  185.     if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) {
  186.         int i;
  187.         int num_colors = 1<<depth;
  188.         gx_color_value rgb[3];
  189.  
  190.         info_ptr->palette = gs_malloc(256 * sizeof (png_color), 1, "png palette");
  191.         if ( info_ptr->palette == 0 )
  192.           { code = gs_note_error(gs_error_VMerror);
  193.             png_write_destroy(png_ptr);
  194.             goto done;
  195.           }
  196.         info_ptr->num_palette = num_colors;
  197.         info_ptr->valid |= PNG_INFO_PLTE;
  198.         for (i=0; i<num_colors; i++) {
  199.         (*dev_proc(pdev, map_color_rgb))((gx_device *)pdev,
  200.                 (gx_color_index)i, rgb);
  201.         info_ptr->palette[i].red = gx_color_value_to_byte(rgb[0]);
  202.         info_ptr->palette[i].green = gx_color_value_to_byte(rgb[1]);
  203.         info_ptr->palette[i].blue = gx_color_value_to_byte(rgb[2]);
  204.         }
  205.     }
  206.  
  207.     /* add comment */
  208.     sprintf(software_text, "%s %d.%02d", gs_product, 
  209.         (int)(gs_revision / 100), (int)(gs_revision % 100));
  210.     text_png.compression = -1;    /* uncompressed */
  211.     text_png.key = (char *)software_key;    /* not const, unfortunately */
  212.     text_png.text = software_text;
  213.     text_png.text_length = strlen(software_text);
  214.     info_ptr->text = &text_png;
  215.     info_ptr->num_text = 1;
  216.  
  217.     /* write the file information */
  218.     png_write_info(png_ptr, info_ptr);
  219.  
  220.     /* don't write the comments twice */
  221.     info_ptr->num_text = 0;
  222.     info_ptr->text = NULL;
  223.  
  224.     /* Write the contents of the image. */
  225.     for ( y = 0; y < height; y++ )
  226.     {    gdev_prn_copy_scan_lines(pdev, y, row, raster);
  227.              png_write_rows(png_ptr, &row, 1);
  228.     }
  229.  
  230.     /* write the rest of the file */
  231.     png_write_end(png_ptr, info_ptr);
  232.  
  233.     /* clean up after the write, and free any memory allocated */
  234.     png_write_destroy(png_ptr);
  235.  
  236.     /* if you malloced the palette, free it here */
  237.     if (info_ptr->palette)
  238.       gs_free(info_ptr->palette, 256 * sizeof (png_color), 1, "png palette");
  239.  
  240. done:
  241.     /* free the structures */
  242.     gs_free((char *)info_ptr, sizeof (png_info), 1, "png info_ptr");
  243.     gs_free((char *)png_ptr, sizeof (png_struct), 1, "png structure");
  244.     gs_free((char *)row, raster, 1, "png raster buffer");
  245.  
  246.     return code;
  247. }
  248.  
  249. /*
  250.  * Patch around a static reference to a never-used procedure.
  251.  * This could be avoided if we were willing to edit pngconf.h to
  252.  *    #undef PNG_PROGRESSIVE_READ_SUPPORTED
  253.  */
  254. #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
  255. #  if PNG_LIBPNG_VER >= 95
  256. #    define PPFB_LENGTH_T png_size_t
  257. #  else
  258. #    define PPFB_LENGTH_T png_uint_32
  259. #  endif
  260. void
  261. png_push_fill_buffer(png_structp png_ptr, png_bytep buffer,
  262.   PPFB_LENGTH_T length)
  263. {
  264. }
  265. #endif
  266.